package nl.infodation.emr.pricingservice.ui;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

import nl.infodation.emr.pricingservice.entity.DomainRule;
import nl.infodation.emr.pricingservice.ui.component.PagingComponent;
import nl.infodation.emr.pricingservice.ui.component.PagingModel;
import nl.infodation.emr.pricingservice.ui.page.SessionData;
import nl.infodation.emr.utility.Constants;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

@SuppressWarnings("serial")
public class PromotionCodePopup extends CustomComponent implements ClickListener {
	
	private ConditionView conditionView ;
	private HorizontalLayout horizontalLayout;
	private VerticalLayout verticalLayout;
	private VerticalLayout mainLayout;
	private PromotionCodeList promotioncodeList;
	private Button btnOk;
	private Button btnCancel;
	
	private PagingComponent pagingComponent;
	private PagingModel model = new PagingModel(0);
	private Label label;
	private List<DomainRule> domainRules;

	public PromotionCodePopup(ConditionView conditionView,
			List<DomainRule> existPromotion) {
		this.conditionView = conditionView;
		this.domainRules = existPromotion;
		buildMainLayout();		
		setCompositionRoot(mainLayout);
		initialModelPaging();
		promotioncodeList.setDataSource(loadItemsFromPage(1));
		btnOk.addListener((ClickListener) this);
		btnCancel.addListener((ClickListener) this);
	}
	
	private void initialModelPaging() {
		setTotalNumberOfPagesForReload();

		model.addPropertyChangeListener("currentPage",
				new PropertyChangeListener() {
					public void propertyChange(final PropertyChangeEvent evt) {
						updatePageNumber();
						promotioncodeList.setDataSource(loadItemsFromPage(model.getCurrentPage()));
					}
				});
		pagingComponent.setModel(model);
		updatePageNumber();
	}	
	
	private List<DomainRule> loadItemsFromPage(int page) {
		List<DomainRule> results = new ArrayList<DomainRule>(); 
		int beginIndex = (page - 1) * promotioncodeList.getPageSize();
		int count = 0;
		while (beginIndex + count < domainRules.size() && count < promotioncodeList.getPageSize()) {
			results.add(domainRules.get(beginIndex + count));
			count++;
		}
		return results;
	}
	
	private void updatePageNumber() {
		label.setValue(SessionData.getI18N().getMessage(
				Constants.CURRENT_PAGE_VALUE)
				+ model.getCurrentPage());
	}
	
	private void setTotalNumberOfPagesForReload() {
		if (null == domainRules) {
			return;
		}
		final long totalItems = domainRules.size();
		final int totalPages = (int) Math.ceil(totalItems
				/ (float) promotioncodeList.getPageSize());
		model.setTotalNumberOfPages(totalPages);
	}
	
	@Override
	public void buttonClick(ClickEvent event) {
		final Button source = event.getButton();
		if(source.equals(btnOk)){
			conditionView.savePromotion();
			((Window) getWindow().getParent()).removeWindow(getWindow());
		}else{
			((Window) getWindow().getParent()).removeWindow(getWindow());
		}
	}
	
	@AutoGenerated
	private void buildMainLayout() {
		// top-level component properties
		setWidth("450px");
		setHeight("400px");
		
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(Boolean.FALSE);
		mainLayout.setSizeFull();
		mainLayout.setMargin(Boolean.FALSE);
		
		verticalLayout = new VerticalLayout();
		verticalLayout.setImmediate(Boolean.FALSE);
		verticalLayout.setWidth("430px");
		verticalLayout.setHeight("70px");
		verticalLayout.setStyleName("promotioncodevertical");
		verticalLayout.setMargin(Boolean.FALSE);
		
		horizontalLayout = new HorizontalLayout();
		horizontalLayout.setImmediate(Boolean.FALSE);
		horizontalLayout.setWidth("350px");		
		horizontalLayout.setMargin(Boolean.FALSE);
		
		promotioncodeList = new PromotionCodeList();
		promotioncodeList.setImmediate(Boolean.FALSE);
		promotioncodeList.setWidth("100.0%");
		promotioncodeList.setHeight("100.0%");
		promotioncodeList.setStyleName("csspromotioncode");
		mainLayout.addComponent(promotioncodeList);
		mainLayout.setExpandRatio(promotioncodeList, 10);
		
		// pagingComponent_1
		pagingComponent = new PagingComponent();
		pagingComponent.setImmediate(Boolean.FALSE);
		pagingComponent.setWidth("-1px");
		pagingComponent.setHeight("-1px");
		verticalLayout.addComponent(pagingComponent);
		
		// label_1
		label = new Label();
		label.setImmediate(Boolean.FALSE);
		label.setWidth("-1px");
		label.setHeight("-1px");
		label.setValue("Label");
		verticalLayout.addComponent(label);
		
		btnOk = new Button();
		btnOk.setCaption("Continue");
		btnOk.setWidth("150px");
		btnOk.setHeight("-1px");
		horizontalLayout.addComponent(btnOk);
		
		btnCancel = new Button();
		btnCancel.setCaption("Cancel");
		btnCancel.setWidth("150px");
		btnCancel.setHeight("-1px");
		horizontalLayout.addComponent(btnCancel);
		
		verticalLayout.addComponent(horizontalLayout);
		
		
		mainLayout.addComponent(verticalLayout);
		mainLayout.setExpandRatio(verticalLayout, 1);
	}
}
